package logic

import (
	"context"
	"errors"

	"go-zero-mall/app/admin/cmd/rpc/internal/svc"
	"go-zero-mall/app/admin/cmd/rpc/pd"
	"go-zero-mall/app/admin/model"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type AdminUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAdminUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUpdateLogic {
	return &AdminUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 管理员-更新
func (l *AdminUpdateLogic) AdminUpdate(in *pd.AdminParamReq) (*pd.Response, error) {
	if in.GetId() == 0 {
		return nil, errors.New("id 不能为空")
	}
	d := new(model.UmsAdmin)
	copier.Copy(&d, in)

	var result int64
	err := l.svcCtx.UmsAdminModel.UpdateNoLoginTime(l.ctx, d)
	if err != nil {
		return nil, errors.New("更新失败")
	} else {
		result = in.Id
	}
	return &pd.Response{Result: result}, nil
}
